<?php 

include('logincheck.php'); 
include('globalvars.php');

if (isset($_POST['username'])){

	mysql_query("DELETE FROM users WHERE facebookid='" . $_SESSION['facebookid'] . "' LIMIT 1");
	$bs = "INSERT INTO users (facebookid,username,company,timezone,format,emailaddress) VALUES ('" . $_SESSION['facebookid'] . "','" . mysql_real_escape_string($_POST['username']) . "','" . mysql_real_escape_string($_POST['company']) . "','" . mysql_real_escape_string($_POST['timezone']) . "','" . mysql_real_escape_string($_POST['format']) . "','" . mysql_real_escape_string($_POST['emailaddress']) . "')";
	mysql_query($bs);
	
	if ($firstTime) header('Location: /performexample.php');
	else header('Location: /');
	exit();
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Create New Appointment Reminder</title>
<link rel="stylesheet" type="text/css" href="form/view.css" media="all">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script type="text/javascript" src="form/view.js"></script>
<script type="text/javascript" src="form/calendar.js"></script>
</head>
<body id="main_body" >
	
	<img id="top" src="form/top.png" alt="">
	<div id="form_container">
	
		<h1><a>header</a></h1>
		<form id="form_216100" class="appnitro"  method="post" action="profile.php">
					<div class="form_description">
			<h2>Update Profile</h2>
			<p>Enter in your profile details</p>
		</div>
			<ul >
                <li id="li_1" >
                    <label class="description" for="username">Your name </label>
                    <div>
                        <input id="username" name="username" class="element text medium" type="text" maxlength="255" value=""/> 
                    </div>
                </li>
                
                <li id="li_2" >
                    <label class="description" for="emailaddress">Your email address</label>
                    <div>
                        <input id="emailaddress" name="emailaddress" class="element text medium" type="text" maxlength="255" value=""/> 
                    </div>
                </li>
                
                <li id="li_3" >
                    <label class="description" for="company">The name of your company or location </label>
                    <div>
                        <input id="company" name="company" class="element text medium" type="text" maxlength="255" value=""/> 
                    </div><p class="guidelines" id="guide_1"><small>Example: "Pink 'n' White"</small></p> 
                </li>
                
                <li id="li_4" >
                    <label class="description" for="defaultformat">Default notification message format </label>
                    <div>
							<textarea id="format" class="element textarea medium" name="format"><?php echo $format; ?></textarea>
                    </div><p class="guidelines" id="guide_3"><small><?php echo $formatvars ?></small></p> 
                </li>
                
                <li id="li_5" >
                	<label class="description" for="timezone">Your timezone </label>
                    <select name="timezone" id="timezone" class="element select">
                          <option value="-10.0">(GMT -10:00) Hawaii</option>
                          <option value="-9.0">(GMT -9:00) Alaska</option>
                          <option value="-8.0">(GMT -8:00) Pacific Time</option>
                          <option value="-7.0">(GMT -7:00) Mountain Time</option>
                          <option value="-6.0">(GMT -6:00) Central Time</option>
                          <option value="-5.0">(GMT -5:00) Eastern Time</option>
                    </select><p class="guidelines" id="guide_2"><small>Ensure this setting is correct as notification timing is dependent on this timezone.</small></p> 
                </li>
   
                <li class="buttons">                 
                    <input id="saveForm" class="button_text" type="submit" name="submit" value="Update" />
                    <a href="/">Cancel and return to dashboard</a>
                </li>
            </ul>
		</form>	
	</div>
	<img id="bottom" src="form/bottom.png" alt="">
    
    <script type="text/javascript">
    
		<?php if (isset($_GET['edit'])) { 
			$result = mysql_query("SELECT * FROM users WHERE facebookid='" . $_SESSION['facebookid'] . "' LIMIT 1");
			$data = json_encode(mysql_fetch_assoc($result));
		?>
		
			var setParams = <?php echo $data; ?>;
			
			for(var key in setParams){
				$("#" + key + "").val(setParams[key]);
			}
			
			selectTimezone(setParams['timezone']);
			
		<?php } else { ?>
			
			$("#username").val("<?php echo $_SESSION['username'] ?>");
			$("#emailaddress").val("<?php echo $_SESSION['emailaddress'] ?>");
			$("#company").val("<?php echo $_SESSION['company'] ?>");
			$("#format").val('<?php echo addslashes($defaultformat) ?>');
			selectTimezone("<?php echo $_SESSION['timezone'] ?>");
			
		<?php } ?>
		$('#form_216100').bind('submit', function(event){
			if (
				$("#username").val().length == 0 ||
				$("#company").val().length == 0 ||
				$("#format").val().length == 0
			){
				alert("Fill out all areas first.");
				return false;
	  		}
			if (!isValidEmailAddress($("#emailaddress").val())){
				alert("Invalid email address");
				return false;
			}
		});
		
		function selectTimezone(timezone){
			$('#timezone').children().each(function() {
				if ($(this).val().indexOf(timezone) != -1)
					$('#timezone').val($(this).val());
			});
		}
    </script>
    
	</body>
</html>